package com.lkl.service.impl;

import com.lkl.bean.BaseReq;
import com.lkl.bean.BaseResp;
import com.lkl.bean.TgTravel;
import com.lkl.client.PayClients;
import com.lkl.dao.TravelRepository;
import com.lkl.service.TravelService;
import com.lkl.utils.AlipayUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.TimeUnit;

/**
 * @Author lkl
 * @Description
 * @Date 2022/3/30
 */
@Service
public class TravelServiceImpl implements TravelService {

    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    RabbitTemplate rabbitTemplate;

    @Autowired
    PayClients payClients;

    @Autowired
    TravelRepository travelRepository;

    static final Logger logger = LoggerFactory.getLogger(TravelServiceImpl.class);

    @Override
    public BaseResp findAll(BaseReq baseReq) {

        Integer page = baseReq.getPage();
        Integer size = baseReq.getSize();
        String name = baseReq.getName();

        if (page == null) {
            return new BaseResp(0, "page为空！", null, null);
        }
        if (size == null) {
            return new BaseResp(0, "size为空！", null, null);
        }
        if (name == null || name.equals("")) {
            return new BaseResp(0, "name为空！", null, null);
        }
        if (page <= 0) {
            page = 1;
        }
        if (size <= 0) {
            size = 10;
        }

        int start = (page - 1) * size;


        Boolean travel_1 = redisTemplate.hasKey("travel_");

        if (travel_1) {
            List travel_ = redisTemplate.opsForList().range("travel_", start, start + size - 1);
            Long total = redisTemplate.opsForList().size("travel_");

            logger.debug("查出的结果数量为：{}" + total);
            return new BaseResp(1, "查询成功！！", total, travel_);
        } else {

            List<TgTravel> all = travelRepository.findAll();

            if (all.size() > 0) {
                redisTemplate.opsForList().rightPushAll("travel_", all);

                logger.debug("redis存入数据成功！{} travel_");


            } else {

                redisTemplate.opsForList().rightPushAll("travel_", "");

                logger.debug("数据库无数据！存入空值{} travel_");
                redisTemplate.expire("travel_", 10, TimeUnit.SECONDS);
            }

            List travel_ = redisTemplate.opsForList().range("travel_", start, start + size - 1);
            Long total = redisTemplate.opsForList().size("travel_");

            logger.debug("查出的结果数量为：{}" + total);
            return new BaseResp(1, "查询成功！！", total, travel_);

        }


    }

    @Override
    public BaseResp pay(Integer id) {

        try {

            rabbitTemplate.convertAndSend("tg-pay-exchange", "pay-travel", id);

            Map map = new HashMap<>();
            map.put("id", id);

            return payClients.travelPay(map);

        } catch (AmqpException e) {
            e.printStackTrace();
            return new BaseResp(0, "网络正忙！", null, null);
        }
    }

    @Override
    public BaseResp findById(Integer id) {

        if (id > 0) {
            Optional<TgTravel> byId = travelRepository.findById(id);
            if (byId.isPresent()) {
                return new BaseResp(1, "查询成功！", null, byId.get());
            }
        }

        return new BaseResp(0, "查询失败！", null, null);
    }
}
